package com.itheima.controller1;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * Author chongge
 * Time 2019/10/29 10:06
 * ClassName com.itheima.controller
 */
@RestController
@RequestMapping(value = "/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;


    /***
     * 获取全部套餐
     * @return
     */
    @RequestMapping(value = "getSetmeal", method = RequestMethod.POST)
    public Result getSetmeal() {
        try {
            //1.首先去redis中查找所有套餐数据
            String setmealListStr = jedisPool.getResource().get("setmealListStr");
            if (setmealListStr == null || "".equals(setmealListStr.trim())) {
                //2.说明redis中没有数据，这时候需要从数据库中查询
                List<Setmeal> setmealList = setmealService.findAll();
                //3.将查询到的集合对象转换为json字符串
                //4.将list集合转换为json格式数据
                setmealListStr = new ObjectMapper().writeValueAsString(setmealList);
                //5.存入redis中
                jedisPool.getResource().set("setmealListStr", setmealListStr);
            }
            //6.将json字符串转换为集合对象
            // 将json字符串转成List<Setmeal>
            ObjectMapper mapper = new ObjectMapper();
            List<Setmeal> setmealList = mapper.readValue(setmealListStr.toString(),
                    new TypeReference<List<Setmeal>>() {
                    });
            return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, setmealList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }


    @RequestMapping(value = "findById")
    public Result findById(Integer id) {
        try {
            //首先从redis中查找对应id存入的套餐数据
            String setmealStr = jedisPool.getResource().get("setmeal_" + id);
            if (setmealStr == null || "".equals(setmealStr.trim())) {
                // 说明redis中没有该对应id存入的套餐数据，需要从数据库中查询
                Setmeal setmeal = setmealService.findById(id);
                // 转换成json字符串存入redis中
                setmealStr = new ObjectMapper().writeValueAsString(setmeal);
                // 存入redis中
                jedisPool.getResource().set("setmeal_" + id, setmealStr);
            }
            // 将json字符串转换成Setmeal对象
            Setmeal setmeal = JSON.parseObject(setmealStr, Setmeal.class);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
}
